var BaseController = new require("./basecontroller");
var md5 = require('MD5');

function Controller(){
	BaseController.call(this);
}

Controller.prototype = Object.create (BaseController.prototype);
Controller.prototype.constructor = Controller;
Controller.prototype.FORM_ENTRIES = Array("branch_id", "company_id", "user_group_id", "username", "password", "email", "firstname", "middlename", "lastname" );
Controller.prototype.SEARCH_ENTRIES = Array("photo", "company_id", "username", "date_added", "company.name", "branch.name");
Controller.prototype.TABLE = "user"; //table for this controller
Controller.prototype.CONTROLLER = "user"; //controllername
Controller.prototype.PAGE = Controller.prototype.CONTROLLER;
Controller.prototype.FORM_VIEW = "backend/user/user-form";
Controller.prototype.LIST_VIEW = "backend/user/user-list";
Controller.prototype.RECORDS_PER_PAGE = 20;
Controller.prototype.VALIDATION_RULES = "users";
Controller.prototype.LIST_JOIN_TABLES = 
				{  "company_id" : //field
					{ 							
						table : "company",//table
						joinType : "left",
						fields: Array('id','name','address')//fields to retrieve
					},
					"branch_id" : //field
					{ 							
						table : "branch",//table
						joinType : "left",
						fields: Array('id','name')//fields to retrieve
					},
					"user_group_id" : //field
					{ 							
						table : "user_group",//table
						joinType : "left",
						fields: Array('id','name')//fields to retrieve
					}
				}; 

Controller.prototype.FORM_JOIN_TABLES = Controller.prototype.LIST_JOIN_TABLES

//for reference data
Controller.prototype.REFERENCE_DATA_LIST = {
		0 : {name : "branchList", query : "select b.id as id, concat(c.name, ' - ', b.name) as name,c.id as company_id from branch b left join company c  on b.company_id = c.id where b.archived = 1 ", appendCustomWhereClause: true},
		1 : {name : "companyList", query : "select id,name  from company where archived = 1 ", appendCustomWhereClause: false},
		2 : {name : "userGroupList", query : "select id,name  from user_group where archived = 1 ", appendCustomWhereClause: true}
}


Controller.prototype.customValidation = function(req, res, data){
	data['errors'] = {};
	data['errors']['confirm_password'] = "HAS ERROR IN CONFIRM PASSWORD!";
	
	return data;
}

Controller.prototype.postBindForm = function(req, res, isFormSubmission, isSearch, retrievedData){
	if(HAS_ROLE('has_manager_role')){
		retrievedData['branch_id'] = req.cookies.BRANCH_ID;
	}
	console.log(11111111111111111);
	if(req.param("password") != null && req.param("password") != ""){
		console.log(333333333333333333);
		retrievedData['password']  = md5(req.param("password"))
	}
	console.log(222222222222222222);
	console.log(JSON.stringify(retrievedData));
}

module.exports = new Controller();
